// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"
	"fmt"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_member_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type MemberInfoDbItem struct {
	ObjectId     string `bson:"_id"` //自动生成
	RoadmapId    string `bson:"roadmapId"`
	MemberUserId string `bson:"memberUserId"`
	JoinTime     int64  `bson:"joinTime"`
	PlanCount    uint32 `bson:"planCount"`
}

func (item *MemberInfoDbItem) ToMemberInfo() *roadmap_member_api.MemberInfo {
	return &roadmap_member_api.MemberInfo{
		MemberUserId: item.MemberUserId,
		JoinTime:     item.JoinTime,
		PlanCount:    item.PlanCount,
	}
}

type _RoadmapMemberDao struct {
	mc *mongo.Collection
}

func newRoadmapMemberDao(db *mongo.Database) (*_RoadmapMemberDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "member")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"roadmapId", 1}, {"joinTime", 1}},
		},
		{
			Keys: bson.D{{"memberUserId", 1}, {"joinTime", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_RoadmapMemberDao{mc: mc}, nil
}

func (db *_RoadmapMemberDao) GenObjectId(roadmapId, memberUserId string) string {
	return fmt.Sprintf("%s:%s", roadmapId, memberUserId)
}

func (db *_RoadmapMemberDao) Insert(ctx context.Context, dbItem *MemberInfoDbItem) error {
	dbItem.ObjectId = db.GenObjectId(dbItem.RoadmapId, dbItem.MemberUserId)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_RoadmapMemberDao) Exist(ctx context.Context, roadmapId, memberUserId string) (bool, error) {
	objectId := db.GenObjectId(roadmapId, memberUserId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": objectId})
	return count > 0, err
}

func (db *_RoadmapMemberDao) Get(ctx context.Context, roadmapId, memberUserId string) (*MemberInfoDbItem, error) {
	objectId := db.GenObjectId(roadmapId, memberUserId)
	retItem := &MemberInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": objectId}).Decode(retItem)
	return retItem, err
}

func (db *_RoadmapMemberDao) Remove(ctx context.Context, roadmapId, memberUserId string) error {
	objectId := db.GenObjectId(roadmapId, memberUserId)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": objectId})
	return err
}

func (db *_RoadmapMemberDao) RemoveByRoadmap(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId})
	return err
}

func (db *_RoadmapMemberDao) CountByRoadmap(ctx context.Context, roadmapId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"roadmapId": roadmapId})
	return uint32(count), err
}

func (db *_RoadmapMemberDao) ListByRoadmap(ctx context.Context, roadmapId string, offset, limit int64) ([]*MemberInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"roadmapId": roadmapId}, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"joinTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*MemberInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RoadmapMemberDao) ListByMember(ctx context.Context, memberUserId string) ([]*MemberInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"memberUserId": memberUserId}, options.Find().SetSort(bson.M{"joinTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*MemberInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RoadmapMemberDao) ListByObjectId(ctx context.Context, objIdList []string) ([]*MemberInfoDbItem, error) {
	if len(objIdList) == 0 {
		return []*MemberInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": objIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*MemberInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RoadmapMemberDao) UpdatePlanCount(ctx context.Context, roadmapId, memberUserId string, planCount uint32) error {
	objectId := db.GenObjectId(roadmapId, memberUserId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": objectId}, bson.M{"$set": bson.M{"planCount": planCount}})
	return err
}
